<?php


namespace App\Http\Controllers\admin\service;

use AlibabaCloud\Client\AlibabaCloud;
use App\Http\Controllers\admin\model\AdminLogModel;
use App\Http\Controllers\admin\model\AdminModel;
use App\Http\Controllers\admin\model\AdminSnModel;
use App\Http\Controllers\admin\model\BatchModel;
use App\Http\Controllers\admin\model\ParentSnModel;
use App\Http\Controllers\admin\model\GroupModel;
use App\Http\Controllers\admin\model\SendTeacherModel;
use App\Http\Controllers\admin\model\SmsModel;
use App\Http\Controllers\admin\model\TeacherSnModel;
use App\Http\Controllers\applets\model\PushLogModel;
use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;
use App\Http\Controllers\utils\UploadUtil;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

/**
 * 账号中心业务层
 * Class AccountCenterService
 * @package App\Http\Controllers\admin\service
 */
class AccountCenterService
{

    /**
     * 验证码登录
     * @param $param
     * @return false|string
     */
    public static function codeLogin($param){

        $param['phone'] = ToolUtil::filterStr($param['phone']);
        $param['code'] = ToolUtil::filterStr($param['code']);

        if(empty($param['type_id'])){
            return ToolUtil::returnJson(401,'类型为空');
        }

        if(!empty($param['type_id'])){
            $type_id = (int)$param['type_id'];
        }else{
            $type_id = 0;
        }

        $code = SmsModel::queryFirst(['phone'=>$param['phone'],'code'=>$param['code'],'type'=>1],['id','create_time','is_use']);
        if(empty($code)){
            return ToolUtil::returnJson(303,'登录验证码不正确');
        }
        if($code['is_use'] === 1){
            return ToolUtil::returnJson(306,'登录验证码已使用过');
        }

        $update_sms = SmsModel::edit(['id'=>$code['id']],['is_use'=>1]);
        if(empty($update_sms)){
            return ToolUtil::returnJson(307,'数据错误');
        }

        if((time()-300) > (int)$code['create_time']){
            return ToolUtil::returnJson(304,'验证码时间超出');
        }

        if($type_id !== 0){
            $admin = AdminModel::queryFirst(['mobile'=>$param['phone'],'t_group_id'=>$type_id],['id','status','login_status','group_id','t_group_id','username','sn_num']);
        }else{
            $admin = AdminModel::queryFirst(['mobile'=>$param['phone'],'t_group_id'=>1],['id','status','login_status','group_id','t_group_id','username','sn_num']);
        }
        
        if(empty($admin)){
            return ToolUtil::returnJson(402,'没有这个账号，请先注册');
        }

        try {

            if($admin['status']===0){
                return ToolUtil::returnJson(301,'账号冻结中，请联系管理员');
            }
            if($admin['status']===2){
                return ToolUtil::returnJson(301,'账号没有通过审核，请联系管理员');
            }
            if($admin['login_status']===1){
                $loginStatus = 2;
            }elseif ($admin['login_status']===2){
                $loginStatus = 1;
            }else{
                $loginStatus = 1;
            }

            $data = AdminModel::queryLogin(['t_admin.id'=>$admin['id']]);
            $param['userId'] = $admin['id'];
            $param['details'] = ToolUtil::handleSuccessOrError(['mobile'=>$param['phone']]);
            LogUtil::addAdminLog($param);
            AdminModel::edit(['id'=>$admin['id']],['login_ip'=>$param['lastIp'],'login_time'=>time(),'login_status'=>$loginStatus]);
            return ToolUtil::returnJson(200,'登录成功',$data);

        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,'登录失败');
        }
        
    }


    /**
     * 发送手机验证码登录的验证码
     * @param $param
     * @return false|string
     */
    public static function getLoginCode($param){

        $phone = ToolUtil::filterStr($param['phone']);
        $tem_code = 'SMS_219390206';

        $res = ToolUtil::aliSms($phone,$tem_code,1);

        return ToolUtil::returnJson($res['code'],$res['msg']);

    }


    /**
     * pc端注册
     * @param $param
     * @return false|string
     */
    public static function register($param){

        $param['username'] = ToolUtil::filterStr($param['username']);
        $param['phone'] = ToolUtil::filterStr($param['phone']);
        $param['code'] = (int)$param['code'];
        $param['type_id'] = (int)$param['type_id'];

        try {
            if($param['password']!==$param['password2']){
                return ToolUtil::returnJson(301,'两次密码不对');
            }

            $username_rec = DB::table('t_admin')->where('t_group_id',$param['type_id'])->where('username',$param['username'])->first(['id']);
            if($username_rec){
                return ToolUtil::returnJson(401,'账号已存在');
            }
            $mobile_rec = DB::table('t_admin')->where('t_group_id',$param['type_id'])->where('mobile',$param['phone'])->first(['id']);
            if($mobile_rec){
                return ToolUtil::returnJson(401,'手机号已存在');
            }

            $t_group = Db::table('t_group')->where('id',$param['type_id'])->first(['group_id']);
            if(empty($t_group) || $t_group['group_id'] === 1 || $t_group['group_id'] === 2){
                return ToolUtil::returnJson(403,'类型错误');
            }

            $code = SmsModel::queryFirst(['phone'=>$param['phone'],'code'=>$param['code']],['id','create_time']);
            if(empty($code)){
                return ToolUtil::returnJson(302,'验证码不正确');
            }

            if((time()-300)>(int)$code['create_time']){
                return ToolUtil::returnJson(304,'验证码时间超出');
            }
            DB::beginTransaction();
            $admin = AdminModel::queryFirstOrWhere(['username'=>$param['username']],['mobile'=>$param['phone']],['id']);
            if($admin){
                return ToolUtil::returnJson(303,'已存在该手机号或账户');
            }
            $randStr = ToolUtil::randStr();

            if($t_group['group_id'] == 3){
                $mobile = AdminModel::queryFirst(['group_id'=>1],['mobile']);
                AccountCenterService::joinClassSms($mobile);
                $status = 2;
            }else{
                $status = 1;
            }

            $data = [
                'username'=>$param['username'],
                'password'=>md5($param['password'].$randStr),
                'mobile'=>$param['phone'],
                'rand_num'=>$randStr,
                'group_id'=>$t_group['group_id'],
                't_group_id'=>$param['type_id'],
                'create_time'=>time(),
                'vip_id'=>4,
                'status'=>$status
            ];
            $param['remark'] = '注册';
            if(AdminModel::add($data)){
                DB::commit();
                return ToolUtil::returnJson(200,'注册成功');
            }
            return ToolUtil::returnJson(300,'注册失败');
            
            
        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,'网络拥堵，请稍后再试');
        }
    }

    /**
     * pc端登录
     * @param $param
     * @return false|string
     */
    public static function login($param){

        $param['username'] = ToolUtil::filterStr($param['username']);
        if(!empty($param['type_id'])){
            $type_id = (int)$param['type_id'];
        }else{
            $type_id = 0;
        }
        
        try {

            $field = ['password','status','rand_num','id'];
            if($type_id !== 0){
                $admin = DB::table('t_admin')->where(['t_group_id'=>$type_id,'username'=>$param['username']])->first($field);
            }else{
                $admin = DB::table('t_admin')->where(['t_group_id'=>1,'username'=>$param['username']])->first($field);
            }
            
            $param['remark'] = '登录';
            if($admin){
                $param['userId'] = $admin['id'];
                if(empty($admin['password'])){
                    return ToolUtil::returnJson(301,'不能用密码登录');
                }
                if($admin['status']==0){
                    return ToolUtil::returnJson(301,'账号被冻结');
                }
                if($admin['status']==2){
                    return ToolUtil::returnJson(301,'账号没有通过审核');
                }
                if($type_id !== 0){
                    $data = AdminModel::queryLogin(['t_admin.t_group_id'=>$type_id,'t_admin.username'=>$param['username'],'t_admin.password'=>md5($param['password'].$admin['rand_num'])]);
                }else{
                    $data = AdminModel::queryLogin(['t_admin.t_group_id'=>1,'t_admin.username'=>$param['username'],'t_admin.password'=>md5($param['password'].$admin['rand_num'])]);
                }
                
                if($data){
                    $param['userId'] = $admin['id'];
                    if($data['login_status']==1){
                        $loginStatus = 2;
                    }elseif($data['login_status']==2){
                        $loginStatus = 1;
                    }else{
                        $loginStatus = 1;
                    }
                    AdminModel::edit(['id'=>$admin['id']],['login_time'=>time(),'login_status'=>$loginStatus,'login_ip'=>$param['lastIp']]);
                    $param['details'] = '用户名：'.$param['username'].',成功';
                    LogUtil::addAdminLog($param);
                    return ToolUtil::returnJson(200,'登录成功',$data);
                }else{
                    return ToolUtil::returnJson(300,'用户名或密码错误');
                }
            }
            return ToolUtil::returnJson(300,'账号没注册');
        }catch (\Exception $exception){
            return ToolUtil::returnJson(301,$exception->getMessage());
        }
    }

    /**
     * 找回密码
     * @param $param
     * @return false|string
     */
    public static function retrievePassword($param){
        $randStr = ToolUtil::randStr();
        $data = [
            'password'=>md5($param['password'].$randStr),
            'rand_num'=>$randStr,
            'update_time'=>time()
        ];
        $sms = SmsModel::queryFirst(['phone'=>$param['mobile'],'code'=>$param['smsCode']],['create_time']);
        if(!$sms){
            return ToolUtil::returnJson(301,'验证码不对或不存在该用户');
        }
        if(time()-300>$sms['create_time']){
            return ToolUtil::returnJson(302,'验证码超过有效期');
        }

        if(empty($param['type_id'])){
            return ToolUtil::returnJson(302,'请选择类型');
        }
        $type_id = (int)$param['type_id'];

        $rec = DB::table('t_admin')->where(['mobile'=>$param['mobile'],'t_group_id'=>$type_id])->first(['id']);
        if(empty($rec)){
            return ToolUtil::returnJson(302,'没有记录');
        }

        $int = AdminModel::edit(['id'=>$rec['id']],$data);
        if($int){
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 注销登录
     * @param $param
     * @return false|string
     */
    public static function loginOut($param){
        try {
            $param['remark'] = '登出';
            $admin = AdminModel::edit(['id'=>$param['userId']],['login_status'=>0,'update_time'=>time()]);
            if($admin){
                $param['details'] = '成功';
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,'登出成功');
            }
            $param['details'] = '失败';
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(300,'登出失败');
        }catch (\Exception $exception){
            return ToolUtil::returnJson(500,'网络拥堵，请稍后再试');
        }
    }

    /**
     * 获取阿里云短信验证码
     * @param $param
     * @return false|string
     * @throws \AlibabaCloud\Client\Exception\ClientException
     * @throws \AlibabaCloud\Client\Exception\ServerException
     */
    public static function smsAli($param){
        $code = rand(100000,999999);
        AlibabaCloud::accessKeyClient(env('ALI_ACCESS_KEY_ID'),env('ALI_ACCESS_KEY_SECRET'))
            ->regionId('cn-hangzhou')->asDefaultClient();
        $result = AlibabaCloud::rpc()
            ->product('Dysmsapi')
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->host('dysmsapi.aliyuncs.com')
            ->options([
                'query' => [
                    'RegionId' => 'cn-hangzhou',    //使用的地区
                    'PhoneNumbers' => "$param[phone]",   //手机号
                    'SignName' => env('MODEL_CODE'),   //签名
                    'TemplateCode' => env('ALI_CODE'),    //模版code
                    'TemplateParam' => "{'code':$code}",   //参数  json格式
                ],
            ])->request();
        if(json_decode($result)->Code=='OK'){
            SmsModel::add(['phone'=>$param['phone'],'code'=>$code,'create_time'=>time()]);
            return ToolUtil::returnJson(200,'短信验证码已发送成功');
        }
        return ToolUtil::returnJson(300,'获取短信验证码失败');
    }

    /**
     * 加入班级短信验证
     * @param $param
     * @return int
     * @throws \AlibabaCloud\Client\Exception\ClientException
     * @throws \AlibabaCloud\Client\Exception\ServerException
     *
     */
    public static function joinClassSms($param){
        AlibabaCloud::accessKeyClient(env('ALI_ACCESS_KEY_ID'),env('ALI_ACCESS_KEY_SECRET'))
            ->regionId('cn-shenzhen')->asDefaultClient();
        $result = AlibabaCloud::rpc()
            ->product('Dysmsapi')
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->host('dysmsapi.aliyuncs.com')
            ->options([
                'query' => [
                    'RegionId' => 'cn-shenzhen',    //使用的地区
                    'PhoneNumbers' => "$param[mobile]",   //手机号
                    'SignName' => env('MODEL_CODE'),   //签名
                    //'TemplateCode' => env('ALI_CODE'),    //模版code
                    'TemplateCode' => 'SMS_215341162',    //模版code
                    'TemplateParam' => "{'code':'a'}",   //参数  json格式
                ],
            ])->request();
        if(json_decode($result)->Code=='OK'){
            return 200;
        }
        return 300;
    }

    /**
     * 获取用户信息
     * @param $param
     * @return false|string
     */
    public static function queryAdminAll($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $count = AdminModel::queryCount([],$param);
                    $page = ToolUtil::calculationPage($param,$count);
                    $data = AdminModel::queryAll([],$param,$page);
                    if(count($data)){
                        return ToolUtil::returnJson(200,'成功',$data,$count);
                    }
                    return ToolUtil::returnJson(400,'没有数据');
                case 2:
                    if(!$param['username']){
                        return ToolUtil::returnJson(401,'用户名为空');
                    }
                    $param['field'] = 'username';
                    $count = AdminModel::queryCount([],$param);
                    $page = ToolUtil::calculationPage($param,$count);
                    $data = AdminModel::queryAll([],$param,$page);
                    if(count($data)){
                        return ToolUtil::returnJson(200,'成功',$data,$count);
                    }
                    return ToolUtil::returnJson(400,'没有数据');
                case 3:
                    if(!$param['mobile']){
                        return ToolUtil::returnJson(401,'手机号为空');
                    }
                    $param['field'] = 'mobile';
                    $count = AdminModel::queryCount([],$param);
                    $page = ToolUtil::calculationPage($param,$count);
                    $data = AdminModel::queryAll([],$param,$page);
                    if(count($data)){
                        return ToolUtil::returnJson(200,'成功',$data,$count);
                    }
                    return ToolUtil::returnJson(400,'没有数据');
            }
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 获取个人信息
     * @param $param
     * @return false|string
     */
    public static function queryPersonalInfo($param){
        $admin = AdminModel::queryFirst(['id'=>$param['id']],['id','username','nickname','mobile','email','avatar','address']);
        if($admin){
            return ToolUtil::returnJson(200,'成功',$admin);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 修改个人信息
     * @param $param
     * @return false|string
     */
    public static function editPersonalInfo($param){
        $username = AdminModel::queryFirst(['id'=>$param['id']],['password','username']);

        if(isset($param['address']) && !empty($param['address'])){
            $address = $param['address'];
        }else{
            $address = null;
        }
        if($username){
            $data = [
                'email'=>$param['email'],
                'nickname'=>$param['nickname'],
                'mobile'=>$param['mobile'],
                'avatar'=>$param['avatar'],
                'address'=>$address,
            ];
            if(isset($param['password']) && $param['password']!=false){
                $randStr = ToolUtil::randStr();
                $data['password'] = md5($param['password'].$randStr);
                $data['rand_num'] = $randStr;

                $sms = SmsModel::queryFirst(['phone'=>$param['mobile'],'code'=>$param['code']],['create_time']);
                if(!$sms){
                    return ToolUtil::returnJson(302,'验证码有误');
                }
                if ((time()-300)>$sms['create_time']){
                    return ToolUtil::returnJson(304,'验证码时间超出');
                }
            }
            $param['remark'] = '修改个人信息';
            if(AdminModel::edit(['id'=>$param['userId']],$data)){
                $param['details'] = ToolUtil::handleSuccessOrError(['username'=>$username['username']]);
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,'成功');
            }
            $param['details'] = ToolUtil::handleSuccessOrError(['username'=>$username['username']],0);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'没有该用户');
    }

    /**
     * 禁用或启用用户
     * @param $param
     * @return false|string
     */
    public static function disableAdmin($param){
        if($param['type']==1){
            $param['remark'] = '禁用用户';
            $status = 0;
        }else{
            $param['remark'] = '启用用户';
            $status = 1;
        }
        $admin = AdminModel::queryFirst(['id'=>$param['id']],['username','group_id']);
        if($admin){
            if($admin['group_id'] === 1){
                return ToolUtil::returnJson(300,$param['remark'].'失败');
            }
            if(AdminModel::edit(['id'=>$param['id']],['status'=>$status,'update_time'=>time()])){
                $param['details'] = '用户名：'.$admin['username'].',成功';
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,$param['remark'].'成功');
            }
            $param['details'] = '用户名：'.$admin['username'].',失败';
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,$param['remark'].'失败');
        }
        return ToolUtil::returnJson(301,'没有该用户');
    }

    /**
     * 审核用户信息
     * @param $param
     * @return false|string
     */
    public static function examineAdmin($param){
        $param['remark'] = '审核用户';
        $admin = AdminModel::queryFirst(['id'=>$param['id']],['username']);
        if($admin){
            if(AdminModel::edit(['id'=>$param['id']],['status'=>$param['status'],'update_time'=>time()])){
                $param['details'] = '用户名：'.$admin['username'].',成功';
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,$param['remark'].'成功');
            }
            $param['details'] = '用户名：'.$admin['username'].',失败';
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,$param['remark'].'失败');
        }
        return ToolUtil::returnJson(301,'没有该用户');
    }

    /**
     * 获取管理日志信息
     * @param $param
     * @return false|string
     */
    public static function queryAdminLog($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryAdminLog([],$param);
                    break;
                case 2:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryAdminLog([],$param);
                    break;
                case 3:
                    $param['field'] = 'al.remark';
                    $param['al.remark'] = $param['remark'];
                    $data = self::handleQueryAdminLog([],$param);
                    break;
                default:
                    $data = self::handleQueryAdminLog([],$param);//
                    break;
            }
        }else{
            $where['al.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryAdminLog($where,$param);
                    break;
                case 2:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryAdminLog($where,$param);
                    break;
                case 3:
                    $param['field'] = 'al.remark';
                    $param['al.remark'] = $param['remark'];
                    $data = self::handleQueryAdminLog($where,$param);
                    break;
                default:
                    $data = self::handleQueryAdminLog($where,$param);//
                    break;
            }
        }
        if(count($data['data'])){
            foreach ($data['data'] as $key=>$value){
                $details = json_decode($value['details'],true);
                if(is_array($details)){
                    try{
                        $value['details'] = implode(",",$details);
                    }catch (\Exception $e){
                        continue;
                    }
                }
                $data['data'][$key] = $value;
            }
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取管理日志信息
     * @param array $where
     * @param $param
     * @return mixed
     */
    public static function handleQueryAdminLog($where=[],$param){
        if($where){
            $data['count'] = AdminLogModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AdminLogModel::queryAll($where,$param,$page);
        }else{
            $data['count'] = AdminLogModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AdminLogModel::queryAll([],$param,$page);
        }
        return $data;
    }

    /**
     * 获取授权信息
     * @param $param
     * @return false|string
     */
    public static function queryEmpowerInfo($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $where['s.status'] = 0;
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryEmpowerInfo($where,$param);
                    break;
                case 2:
                    $param['field'] = 's.sn_value';
                    $param['s.sn_value'] = $param['snCode'];
                    $data = self::handleQueryEmpowerInfo($where,$param);
                    break;
                case 3:
                    $param['field'] = 'name';
                    $data = self::handleQueryEmpowerInfo($where,$param);
                    break;
                default:
                    $data = self::handleQueryEmpowerInfo($where,$param);//
                    break;
            }
        }else{
            $where['s.teacher_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryEmpowerInfo($where,$param);
                    break;
                case 2:
                    $param['field'] = 's.sn_value';
                    $param['s.sn_value'] = $param['snCode'];
                    $data = self::handleQueryEmpowerInfo($where,$param);
                    break;
                case 3:
                case 4:
                    $param['field'] = 'name';
                    $data = self::handleQueryEmpowerInfo($where,$param);
                    break;
                default:
                    $data = self::handleQueryEmpowerInfo($where,$param);//
                    break;
            }
        }
        if($data['data']){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取授权信息
     * @param array $where
     * @param $param
     * @return mixed
     */
    public static function handleQueryEmpowerInfo($where=[],$param){
        if($where){
            $data['count'] = SendTeacherModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = SendTeacherModel::queryAll($where,$param,$page);
        }else{
            $data['count'] = SendTeacherModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = SendTeacherModel::queryAll([],$param,$page);
        }
        return $data;
    }

    /**
     * 是否确定授权
     * @param $param
     * @return false|string
     */
    public static function empowerExamine($param){
        return ToolUtil::returnJson(300,'弃用');//弃用
        if($param['type']==1){
            $status = 1;
        }else{
            $status = 2;
        }
        DB::beginTransaction();
        $sCount = SendTeacherModel::edit(['id'=>$param['id']],['status'=>$status]);
        if($sCount){
            if($status==1){
                $sendTeacher = SendTeacherModel::queryFirst(['id'=>$param['id']],['sn_value','teacher_id','batch_rand']);
                if($sendTeacher){
                    $snId = AdminSnModel::valueGetId(['value'=>$sendTeacher['sn_value']]);
                    $batchId = BatchModel::whereField(['rand_code'=>$sendTeacher['batch_rand']]);
                    $tCount = TeacherSnModel::add(['admin_id'=>$sendTeacher['teacher_id'],'admin_sn_id'=>$snId,'batch_id'=>$batchId]);
                    if(!$tCount){
                        DB::rollBack();
                        return ToolUtil::returnJson(300,'失败');
                    }
                    $param['remark'] = '授权';
                    $param['status'] = 1;
                    $param['type'] = 2;
                    $param['contentType'] = 17;
                    $param['statusType'] = '授权信息';
                    $param['batch'] = array($sendTeacher['sn_value']);
                    $param['details'] = ToolUtil::handleSuccessOrError(['sn码'=>$sendTeacher['sn_value']]);
                    LogUtil::addAdminLog($param);
                    $jPush = LogUtil::addPushLog($param);
                    DB::commit();
                    if(in_array(200,$jPush)){
                        return ToolUtil::returnJson(200,'成功');
                    }
                    return ToolUtil::returnJson(200,'成功,推送失败');
                }
                return ToolUtil::returnJson(301,'不存在该信息');
            }else{
                DB::commit();
                return ToolUtil::returnJson(200,'成功');
            }
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取老师信息
     * @param $param
     * @return false|string
     */
    public static function queryTeacherInfo($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin['group_id']==1 || $admin['group_id']==2){
            $where['group_id'] = 3;
            switch($param['type']){
                case 1:
                    $data = self::handleQueryTeacherInfo($where,$param);
                    break;
                case 2:
                    if(!$param['username']){
                        return ToolUtil::returnJson(401,'用户名为空');
                    }
                    $param['field'] = 'username';
                    $data = self::handleQueryTeacherInfo($where,$param);
                    break;
                case 3:
                    if(!$param['mobile']){
                        return ToolUtil::returnJson(401,'手机号为空');
                    }
                    $param['field'] = 'mobile';
                    $data = self::handleQueryTeacherInfo($where,$param);
                    break;
                default:
                    $data = self::handleQueryTeacherInfo([],$param);//
                    break;
            }
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 获取家长账号信息
     * @param $param
     * @return false|string
     */
    public static function queryParentInfo($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin['group_id']==1 || $admin['group_id']==2){
            $where['group_id'] = 4;
            switch($param['type']){
                case 1:
                    $data = self::handleQueryTeacherInfo($where,$param);
                    break;
                case 2:
                    if(!$param['username']){
                        return ToolUtil::returnJson(401,'用户名为空');
                    }
                    $param['field'] = 'username';
                    $data = self::handleQueryTeacherInfo($where,$param);
                    break;
                case 3:
                case 4:
                    if(!$param['mobile']){
                        return ToolUtil::returnJson(401,'手机号为空');
                    }
                    $param['field'] = 'mobile';
                    $data = self::handleQueryTeacherInfo($where,$param);
                    break;
                default:
                    $data = self::handleQueryTeacherInfo([],$param);//
                    break;
            }
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取老师信息
     * @param array $where
     * @param $param
     * @return mixed
     */
    public static function handleQueryTeacherInfo($where=[],$param){
        if($where){
            $data['count'] = AdminModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AdminModel::queryAll($where,$param,$page);
        }else{
            $data['count'] = AdminModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AdminModel::queryAll([],$param,$page);
        }
        return $data;
    }

    /**
     * 上传头像
     * @param $param
     * @return false|string
     */
    public static function uploadAvatar($param){
        $param['size'] = 1*1024*1024;
        $param['width'] = 800;
        $param['height'] = 800;
        return UploadUtil::uploadImg($param,$param['file']);
    }

    /**
     * 获取推送日志信息
     * @param $param
     * @return false|string
     */
    public static function queryPushLog($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryPushLog([],$param);
                    break;
                case 2:
                    $param['field'] = 'p.remark';
                    $param['p.remark'] = $param['remark'];
                    $data = self::handleQueryPushLog([],$param);
                    break;
                case 3:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryPushLog([],$param);
                    break;
                default:
                    $data['data'] = null;
            }
        }else{
            $where['p.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryPushLog($where,$param);
                    break;
                case 2:
                    $param['field'] = 'p.remark';
                    $param['p.remark'] = $param['remark'];
                    $data = self::handleQueryPushLog($where,$param);
                    break;
                case 3:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryPushLog($where,$param);
                    break;
                default:
                    $data['data'] = null;
            }
        }
        if(count($data['data'])){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取的推送日志信息
     * @param array $where
     * @param $param
     * @return mixed
     */
    public static function handleQueryPushLog($where=[],$param){
        if($where){
            $data['count'] = PushLogModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = PushLogModel::queryAll($where,$param,$page);
        }else{
            $data['count'] = PushLogModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = PushLogModel::queryAll([],$param,$page);
        }
        return $data;
    }

    /**
     * 获取权限信息
     * @param $param
     * @return false|string
     */
    public static function queryGroup($param){
        switch ($param['type']){
            case 1:
                $count = GroupModel::queryCount([],$param);
                $page = ToolUtil::calculationPage($param,$count);
                $data = GroupModel::queryAll([],$param,$page);
                break;
            case 2:
                $param['field'] = 'title';
                $count = GroupModel::queryCount([],$param);
                $page = ToolUtil::calculationPage($param,$count);
                $data = GroupModel::queryAll([],$param,$page);
                break;
            default:
                $count = 0;
                $data = [];
        }
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data,$count);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取权限信息
     * @param $param
     * @return false|string
     */
    public static function queryIdGroup($param){
        $field = ['id','title','roule'];
        $data = GroupModel::queryFirst(['id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑权限信息
     * @param $param
     * @return false|string
     */
    public static function editGroup($param){
        $param['remark'] = '编辑权限设置';
        $data = [
            'title'=>$param['title'],
            'roule'=>$param['roule'],
            'update_time'=>time()
        ];
        $int = GroupModel::edit(['id'=>$param['id']],$data);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['groupName'=>$param['title']]);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }
}